<?php
declare(strict_types=1);

namespace App\Admin\Mapper\System;

use App\Admin\Model\System\SystemApiLog;
use Illuminate\Database\Eloquent\Builder;
use Newbie\Abstracts\AbstractMapper;

/**
 * Class SystemApiMapper
 * @package App\System\Mapper
 */
class SystemApiLogMapper extends AbstractMapper {
    /**
     * @var SystemApiLog
     */
    public $model;

    public function assignModel (): void {
        $this->model = SystemApiLog::class;
    }

    /**
     * 搜索处理器
     * @param Builder $query
     * @param array $params
     * @return Builder
     */
    public function handleSearch (Builder $query, array $params): Builder {
        if (isset($params['api_name'])) {
            $query->where('api_name', 'like', '%' . $params['api_name'] . '%');
        }
        if (isset($params['access_name'])) {
            $query->where('access_name', 'like', '%' . $params['access_name'] . '%');
        }
        if (isset($params['minDate']) && isset($params['maxDate'])) {
            $query->whereBetween(
                'access_time',
                [$params['minDate'] . ' 00:00:00', $params['maxDate'] . ' 23:59:59']
            );
        }
        return $query;
    }
}